<?php

namespace App\Http\Controllers;

use App\Models\Clue;
use App\Models\MediaSource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class HomeController extends Controller
{
    public function page404()
    {
        return abort(404);
    }

    public function pageLookIp()
    {
        return request()->ip();
    }

    public function hashString(Request $request)
    {
        $str = $request->get('str');
        if (!$str) return '';
        $key = '3322123';
        return response()->json([
            'result' => my_encrypt($str, $key)
        ]);
    }

    public function pageFillClue(Request $request)
    {
        $key = '3322123';
        $id = my_decrypt($request->get('k'), $key);
        if (!is_numeric($id) || !$source = MediaSource::query()->where('id', $id)->first())
            return abort(404);

        return Inertia::render("Clue/FillClue", [
            'id' => $id,
            'token' => csrf_token(),
            'title' => $source['name'],
        ])
            ->rootView('app')
            ->toResponse(request())
            ->content();
    }
}
